package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
//        先插入Setmeal'
        Setmeal s = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, s);

        s.setStatus(StatusConstant.ENABLE);
        setmealMapper.add(s);
        Long id = s.getId();
//        在插入Setmeal—dish

        List<SetmealDish> Dishes = setmealDTO.getSetmealDishes();
        for (SetmealDish Dish : Dishes) {
            Dish.setSetmealId(id);
        }
        setmealDishMapper.add(Dishes);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
//更新套餐表
        Setmeal s = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, s);
        setmealMapper.update(s);

        List<SetmealDish> Dishes = setmealDTO.getSetmealDishes();
        for (SetmealDish Dish : Dishes) {
            Dish.setSetmealId(s.getId());
        }
//        删除套餐菜品
        List<Long> idList = new ArrayList<>();
        idList.add(s.getId());
        setmealDishMapper.delete(idList);
//        新增菜品
        setmealDishMapper.add(Dishes);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal s=Setmeal.builder()
                        .status(status)
                        .id(id)
                        .build();
        setmealMapper.update(s);
    }

    @Override
    public PageResult pagefind(SetmealPageQueryDTO c) {
        PageHelper.startPage(c.getPage(), c.getPageSize());

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(c, setmeal);
        Page<SetmealVO> p=setmealMapper.find(setmeal);
        PageResult pageBean=new PageResult(p.getTotal(),p.getResult());
        return  pageBean;
    }

    @Override
    public SetmealVO findById(Long id) {
//        从Setmeal中找
        SetmealVO sVO =setmealMapper.findById(id);

//        从Setmealdish中找
        sVO.setSetmealDishes(setmealDishMapper.findBySetMealId(id));

        return sVO;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
//        批量删除setmeal
        setmealMapper.delete(ids);
//        批量删除setmealdish

        setmealDishMapper.delete(ids);
    }

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId) {
        return setmealMapper.findByCategoryId(categoryId);
    }
}
